package org.socialresume.client.mvp.view;

import org.socialresume.client.model.Feed;
import org.socialresume.client.model.FeedItem;
import org.socialresume.client.model.TwitterFeed;
import org.socialresume.client.mvp.presenter.FeedPresenter;
import org.socialresume.client.mvp.view.widget.TwitterUpdateWidget;
import org.socialresume.client.mvp.view.widget.UpdateWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class FeedUI extends Composite implements FeedPresenter.Display {

	private static FeedUIUiBinder uiBinder = GWT.create(FeedUIUiBinder.class);

	interface FeedUIUiBinder extends UiBinder<Widget, FeedUI> {
	}
	
	@UiField
	Panel feedsContainer;
	
	@UiField
	Panel loadingContainer;

	private VisibilityWrapper loadingWidgetWrapper;

	public FeedUI() {
		initWidget(uiBinder.createAndBindUi(this));
		
		loadingWidgetWrapper = new VisibilityWrapper(loadingContainer);
		loadingWidgetWrapper.setValue(false);
	}

	@Override
	public void clear() {
		feedsContainer.clear();
		feedsContainer.setVisible(false);
	}

	@Override
	public void addFeed(Widget feed) {
		feedsContainer.add(feed);
		feedsContainer.setVisible(true);
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasValue<Boolean> getLoadingVisibility() {
		return loadingWidgetWrapper;
	}

	@Override
	public Widget getFeedItemWidget(Feed feed, FeedItem item) {
		switch(feed.getType()){
		case TWITTER:
			return new TwitterUpdateWidget((TwitterFeed) feed, item);
		default:
			return new UpdateWidget(feed, item);
		}
	}
}
